// pages/wb/pay.js
const numeral = require('../../vendor/numeral/numeral.min.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        time: '30:00',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        const app = getApp();
        const pd = app.getTmpPlate();
        this.setData({
            pd,
        })
        app.http.post('rest/plate-number/price?type=' + pd.type.replace(/\D/g, ''), {
            showMsg: false
        }).then(res => {
            this.setData({
                price: numeral(res.content.price).format('0.00'),
                id: res.content.id,
            })
        }).catch(err => {
            console.log(err);
            err.code == 418 && wx.showModal({
                title: '提示',
                content: '系统暂时无法接单，请稍后再试！',
                showCancel: false,
                success: res => {
                    if (res.confirm) {
                        wx.redirectTo({
                            url: '/pages/car-owner/index',
                        })
                    }
                }
            })
        });
    },

    onReady() {
        let time = 1800;
        let id = setInterval(() => {
            time--;
            const m = Math.floor(time / 60);
            const s = time % 60;
            this.setData({
                time: [m < 10 ? ('0' + m) : m, s < 10 ? ('0' + s) : s].join(':')
            });
            if (time <= 0) {
                clearInterval(id);
            }
        }, 1000);

    },
    submit() {
        const data = Object.assign({
            plateNumberId: this.data.id
        }, this.data.pd);
        data.information = JSON.stringify(data.files);
        wx.showLoading({
            title: '加载中...',
        })
        getApp().http.post('rest/plate-number-order/create', {
            data,
            showMsg: false
        }).then(res => {
            wx.hideLoading();
            wx.showToast({
                icon: 'success',
                title: '下单成功！',
                success:res=>{
                    setTimeout(()=>{
                        wx.redirectTo({
                            url: '/pages/car-owner/index',
                        })
                    },1000)
                }
            });
            // 跳转
        }).catch(err => {
            err.code == 11001 && wx.showModal({
                title: '提示',
                content: '账户余额不足，请充值',
                success: res => {
                    if (res.confirm) {
                        wx.navigateTo({
                            url: '/pages/common/recharge',
                        })
                    } else if (res.cancel) {
                        console.log('用户点击取消')
                        wx.redirectTo({
                            url: '/pages/car-owner/index',
                        })
                    }
                }
            })
        })
    }
})